package collection.stream;

import wrapper.IntegerDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * JDK8之后，集合提供了流式操作
 * stream() 将集合变成流，流中的内容就是集合中的元素
 */
public class StreamApiDemo {
    public static void main(String[] args) {
        Random random  =new Random();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(random.nextInt(100));
        }
        System.out.println("原始数据："+list);
        List<Integer> list1 = list.stream().map(e->e*3).collect(Collectors.toList());
        System.out.println("扩大三倍:"+list1);
        List<Integer> list2 = list1.stream().filter(e->e%2==0).collect(Collectors.toList());
        System.out.println("滤掉奇数留下偶数："+list2);
        List<Integer> list3 = list2.stream().distinct().collect(Collectors.toList());
        System.out.println("去重："+list3);
        List<Integer> list4 = list3.stream().sorted((e1,e2)->e2-e1).collect(Collectors.toList());
        System.out.println("降序："+list4);
        List<Integer> list5 = list.stream().map(e->e*3).filter(e->e%2==0).distinct().sorted((e1,e2)->e2-e1).collect(Collectors.toList());
        System.out.println("综合:"+list5);
    }
}
